package com.net.request

import com.net.NetUtils
import okhttp3.FormBody
import okhttp3.Request
import okhttp3.RequestBody

class PostFormRequestBuilder : BaseRequestBuilder() {
    override fun build(): NetUtils {
        val build = Request.Builder()
        //URL 参数
        build.url(url)
        //请求tag
        tag?.let {
            build.tag(tag)
        }
        //请求header
        headers?.let {
            build.headers(generateHeader(it))
        }
        //请求方式
        build.post(getFormBody(params))
        return NetUtils(build.build())
    }

    private fun getFormBody(params: MutableMap<String, String>?): RequestBody {
        val builder = FormBody.Builder()
        params?.forEach { (key, value) ->
            builder.add(key, value)
        }
        return builder.build()
    }

}
